package com.atguigu.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.atguigu.entity.BaseCategoryView;
import com.atguigu.mapper.BaseCategoryViewMapper;
import com.atguigu.service.BaseCategoryViewService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * <p>
 * VIEW 服务实现类
 * </p>
 *
 * @author zhangqiang
 * @since 2021-08-27
 */
@Service
public class BaseCategoryViewServiceImpl extends ServiceImpl<BaseCategoryViewMapper, BaseCategoryView> implements BaseCategoryViewService {

    @Override
    public List<JSONObject> getIndexCategoryInfo() {
        List<JSONObject> parentCategoryList = new ArrayList<>();
        //一级分类
        List<BaseCategoryView> baseCategoryViews = baseMapper.selectList(null);
        Map<Long, List<BaseCategoryView>> collect1Map = baseCategoryViews.stream().collect(Collectors.groupingBy(BaseCategoryView::getCategory1Id));
        int index=1;
        for (Map.Entry<Long, List<BaseCategoryView>> longListEntry : collect1Map.entrySet()) {
            JSONObject category1List = new JSONObject();
            Long category1Id = longListEntry.getKey();
            List<BaseCategoryView> Category1value = longListEntry.getValue();
            String category1Name = Category1value.get(0).getCategory1Name();
            category1List.put("index",index++);
            category1List.put("categoryId",category1Id);
            category1List.put("categoryName",category1Name);
            //找到所有的二级分类
            Map<Long, List<BaseCategoryView>> collect2Map = Category1value.stream().collect(Collectors.groupingBy(BaseCategoryView::getCategory2Id));
            List<JSONObject> category1Children = new ArrayList<>();
            for (Map.Entry<Long, List<BaseCategoryView>> listEntry : collect2Map.entrySet()) {
                //构造一个json对象把二级分类信息封装进去
                JSONObject category2List = new JSONObject();
                Long category2Id = listEntry.getKey();
                List<BaseCategoryView> category2 = listEntry.getValue();
                String category2Name = category2.get(0).getCategory2Name();
                category2List.put("categoryId",category2Id);
                category2List.put("categoryName",category2Name);
                List<JSONObject> category2Children = new ArrayList<>();
                category2.stream().forEach(category3->{
                    JSONObject category3List = new JSONObject();
                    category3List.put("categoryId",category3.getCategory3Id());
                    category3List.put("categoryName",category3.getCategory3Name());
                    category2Children.add(category3List);
                });
                category2List.put("categoryChild",category2Children);
                category1Children.add(category2List);
            }
            category1List.put("categoryChild",category1Children);
            parentCategoryList.add(category1List);
        }

        return parentCategoryList;
    }
}
